package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameRankMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameActivity;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRank;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_GROUP_TOP_USER, desc = "各分组前50玩家")
public class ChannelEnterNameGroupTopUserCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        EnterNameProto.ChannelEnterNameGroupTopUserReqMsg reqMsg = EnterNameProto.ChannelEnterNameGroupTopUserReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = 0;
        if(reqMsg.hasType()){
            type = reqMsg.getType();
        }
        if(type == 0){
            EnterNameActivity activity = ChannelEnterNameMgr.getEnterNameActivity(activityId);
            if(activity != null){
                type = activity.getUserConditionType();
            }
        }
        if(type == 0){
            //默认牧场点兵
            type = eGamePlayerEventType.ChannelPastureMaxScoreRank.getValue();
        }
        int groupId = 0;
        if(reqMsg.hasGroupId()){
            groupId = reqMsg.getGroupId();
        }
//        System.out.println("===========================reqMsg.getZoneId()  " + reqMsg.getZoneId());
        EnterNameProto.EnterNameGroupTopUserRespMsg.Builder clientMsg = EnterNameProto.EnterNameGroupTopUserRespMsg.newBuilder();
        clientMsg.setRet(0);
        EnterNameActivity activity = ChannelEnterNameMgr.getEnterNameActivity(activityId);
        EnterNameModel model = ChannelEnterNameMgr.getEnterNameModelMap().get(activityId);
        if(activity != null && model != null){
            if(reqMsg.hasZoneId()){
                //按地区读取，返回单个地区
                EnterNameRankModel rankModel = ChannelEnterNameRankMgr.getEnterNameRankModel(activityId, type, reqMsg.getZoneId());
                if(rankModel != null){
                    EnterNameProto.EnterNameGroupTopUserMsg.Builder myGroupList = EnterNameProto.EnterNameGroupTopUserMsg.newBuilder();
                    for (EnterNameRank nameRank : rankModel.getRankList()) {
                        XRankProto.XUserRankMsg.Builder builder = ChannelEnterNameRankMgr.buildRankMsg(nameRank);
                        myGroupList.addRank(builder);
                    }
                    myGroupList.setGroupId(model.getGroupId(reqMsg.getZoneId()));
                    clientMsg.addGroup(myGroupList);
                }
            }else {
                //按分组读取，返回两个地区
                if(groupId == 0){
                    UserEnterName userEnterName = model.getUserEnterName(reqMsg.getUserKey().getUserId());
                    //拿分组信息
                    if(userEnterName != null) {
                        groupId = userEnterName.getGroupId();
                    }
                }
                ActivityGroupMatch groupMatch = model.getActivityGroupMatch(groupId);
                if(groupMatch != null){
                    for (Integer zId : groupMatch.getZoneIdList()) {
                        EnterNameRankModel rankModel = ChannelEnterNameRankMgr.getEnterNameRankModel(activityId, type, zId);
                        if(rankModel != null){
                            EnterNameProto.EnterNameGroupTopUserMsg.Builder myGroupList = EnterNameProto.EnterNameGroupTopUserMsg.newBuilder();
                            for (EnterNameRank nameRank : rankModel.getRankList()) {
                                XRankProto.XUserRankMsg.Builder builder = ChannelEnterNameRankMgr.buildRankMsg(nameRank);
                                myGroupList.addRank(builder);
                            }
                            myGroupList.setGroupId(model.getGroupId(zId));
                            clientMsg.addGroup(myGroupList);
                        }
                    }
                }
            }
        }
        ChannelMessageHelper.sendGamePacket(Protocol.U_ENTER_NAME_GROUP_TOP_USER, clientMsg, reqMsg.getUserKey().getChannelId(),
                reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());
    }

    @Override
    public void sendData() {

    }
}
